<?php
/**
 * AuthController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Auth
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/** auth adapter */
require_once 'Zend/Auth/Adapter/DbTable.php';

/** openid adapter */
require_once 'Zend/Auth/Adapter/OpenId.php';

/** OpenID consumer */
require_once "Zend/OpenId/Consumer.php";

/** OpenID storage */
require_once "Zend/OpenId/Consumer/Storage/File.php";

/**
 * Auth_AuthController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Auth
 */
class Auth_AuthController extends Madoqua_Controller_Action_Abstract
{
    /**
     * auth instance
     *
     * @var Zend_Auth
     */
    private $_auth;

    /**
     * auth adapter
     *
     * @var Zend_Auth_Adapter_DbTable
     */
    private $_dbTableAdapter;

    /**
     * openid auth adapter
     *
     * @var Zend_Auth_Adapter_OpenId
     */
    private $_openIdAdapter;

    /**
     * openid consumer
     *
     * @var Zend_OpenId_Consumer
     */
    private $_consumer;

    /**
     * initialization
     */
    public function init()
    {
        parent::init();
        //spotty doesn't like to bubble :(

        $this->_auth = Zend_Auth::getInstance();
        //the auth object

        $table = Madoqua_Application::getConfiguration()->database->prefix . 'user';
        $this->_dbTableAdapter = new Zend_Auth_Adapter_DbTable($this->_db, $table, 'username', 'password');
        //adapter for "regular" logins using dbtable

        $dir = Madoqua_Application::getDataDirectory() . DIRECTORY_SEPARATOR . 'openid/';
        $storage = new Zend_OpenId_Consumer_Storage_File($dir);
        $this->_openIdAdapter = new Zend_Auth_Adapter_OpenId(null, $storage);
        //adapter for openid, using custom storage on the file system

        $this->_consumer = new Zend_OpenId_Consumer($storage, true);
        //OpenID consumer
        //FIXME remove?
    }

    /**
     * logging in :)
     */
    public function loginAction()
    {
        $loginForm = $this->_formLoader->get('Auth_Auth_Login', array('auth' => $this->_auth, 'adapter' => $this->_dbTableAdapter));
        $this->view->loginForm = $loginForm;
        //regular login

        $openIDForm = $this->_formLoader->get('Auth_Auth_Openid', array('auth' => $this->_auth, 'adapter' => $this->_openIdAdapter));
        $this->view->openIDForm = $openIDForm;
        //OpenID login

        if ($loginForm->isPosted() && $loginForm->isValid($this->getRequest()->getPost())) {
            //valid login
            $this->_auth->getStorage()->write($this->_dbTableAdapter->getResultRowObject());
            //store user row object
            $this->getHelper('redirector')->gotoRoute(array(), 'blog_admin');
            //hmmz

        } elseif ($openIDForm->isPosted() && $openIDForm->isValid($this->getRequest()->getPost())) {
            //open id is valid
            //nothing has to happen (consumer will cause redirect)
        } elseif (($mode = $this->getRequest()->getQuery('openid_mode')) !== null) {
            //the redirect has happened! woo!

            $result = $this->_auth->authenticate($this->_openIdAdapter);
            //try to authenticate

            if ($result->isValid()) {
                $this->_modelLoader->load('Front_User');
                $userModel = new Front_User();
                $user = $userModel->fetchRow($userModel->select()->where('openid = ?', rtrim($result->getIdentity(), '/')));
                //fetch the matchin user row

                $this->_auth->getStorage()->write((object) $user);
                //store the user object

                $this->getHelper('redirector')->gotoRouteAndExit(array(), 'blog_admin', true);
                //burpz
            } else {
                $this->view->failed = true;
            }

        } elseif ($this->getRequest()->isPost()) {
            //user did post, but both forms were invalid
            $this->view->failed = true;
        }
    }

    /**
     * and logging back out :x
     */
    public function logoutAction()
    {
        $this->_auth->clearIdentity();
        //wooo! erase, erase, erase!
        $this->getHelper('redirector')->gotoRoute(array(), 'home');
        //send 'm back home
    }
}